package com.wengzw.service;

import com.wengzw.dao.CustomerMapper;
import com.wengzw.po.Customer;
import com.wengzw.vo.CustomerVo;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
@Service
@Transactional
public class CustomerServiceImpl implements CustomerService {
    @Resource
    private CustomerMapper customerMapper;
    @Select("SELECT\n" +
            "\tcust_id,\n" +
            "\tcustname,\n" +
            "\tcust_user_id,\n" +
            "\tcust_create_id,\n" +
            "\ta.dict_item_name source,\n" +
            "\tb.dict_item_name industry,\n" +
            "\td.dict_item_name cust_level,\n" +
            "\tlinkman,\n" +
            "\tphone,\n" +
            "\tmoblie,\n" +
            "\tzipcode,\n" +
            "\taddress,\n" +
            "\tcreatetime\n" +
            "FROM\n" +
            "\tcustomer c\n" +
            "\tLEFT JOIN\n" +
            "\t(SELECT dict_id,dict_item_name FROM base_dict where dict_type_code = '001') a on c.industry = a.dict_id\n" +
            "\tLEFT JOIN\n" +
            "\t(SELECT dict_id,dict_item_name FROM base_dict where dict_type_code = '002') b on c.source = b.dict_id\n" +
            "\tLEFT JOIN\n" +
            "\t(SELECT dict_id,dict_item_name FROM base_dict where dict_type_code = '003') d on c.cust_level = d.dict_id\n" +
            "        <where>\n" +
            "            <if test=\"custname != null and custname != ''\">\n" +
            "                custname like \"%\"#{custname}\"%\"\n" +
            "            </if>\n" +
            "            <if test=\"source != null and source != ''\">\n" +
            "                and source = #{source}\n" +
            "            </if>\n" +
            "            <if test=\"industry != null and industry != ''\">\n" +
            "                and industry = #{industry}\n" +
            "            </if>\n" +
            "            <if test=\"cust_level != null and cust_level != ''\">\n" +
            "                and cust_level = #{cust_level}\n" +
            "            </if>\n" +
            "        </where>")
    public List<Customer> findCustomerList(CustomerVo customerVo) {
        return customerMapper.findCustomerList(customerVo);
    }
    //添加客户
    @Insert("insert into customer(custname,cust_user_id,cust_create_id,source,industry,cust_level,linkman,phone,moblie,zipcode,address)\n" +
            "\t    values (#{custname},#{cust_user_id},#{cust_create_id},#{source},#{industry},#{cust_level},\n" +
            "\t    #{linkman},#{phone},#{moblie},#{zipcode},#{address})")
    public int addCustomer(Customer customer){
        return customerMapper.addCustomer(customer);
    }
    //修改客户
    @Update("update customer\n" +
            "\t\t<set>\n" +
            "\t\t\t<if test=\"custname!=null \">\n" +
            "\t\t\t\tcustname = #{custname},\n" +
            "\t\t\t</if>\n" +
            "\t\t\t<if test=\"source!=null \">\n" +
            "\t\t\t\tsource = #{source},\n" +
            "\t\t\t</if>\n" +
            "\t\t\t<if test=\"industry!=null \">\n" +
            "\t\t\t\tindustry = #{industry},\n" +
            "\t\t\t</if>\n" +
            "\t\t\t<if test=\"cust_level!=null \">\n" +
            "\t\t\t\tcust_level = #{cust_level},\n" +
            "\t\t\t</if>\n" +
            "\t\t\t<if test=\"phone!=null\">\n" +
            "\t\t\t\tphone = #{phone},\n" +
            "\t\t\t</if>\n" +
            "\t\t\t<if test=\"moblie!=null\">\n" +
            "\t\t\t\tmoblie = #{moblie},\n" +
            "\t\t\t</if>\n" +
            "\t\t\t<if test=\"zipcode!=null\">\n" +
            "\t\t\t\tzipcode = #{zipcode},\n" +
            "\t\t\t</if>\n" +
            "\t\t\t<if test=\"address!=null\">\n" +
            "\t\t\t\taddress = #{address},\n" +
            "\t\t\t</if>\n" +
            "\t\t</set>\n" +
            "\t\twhere cust_id = #{cust_id}")
    public int updateCustomer(Customer customer){
        return customerMapper.updateCustomer(customer);
    }

    //删除客户
    @Delete("delete from customer where cust_id = #{cust_id}")
    public int deleteById(Integer cust_id){
        return customerMapper.deleteById(cust_id);
    }

    //根据id查询用户
    @Override
    public Customer findCustomerByID(Integer cust_id) {
        return customerMapper.findCustomerByID(cust_id);
    }
}
